/**
 * 
 */
package com.toncen.samepms.common;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sky.common.properties.PropertiesUtils;

/**
 * 系统常用常量和公共方法等
 * 
 * @author tch
 * 
 */
public final class ComitConstant {
	/**
	 */
	private static Log logger = LogFactory.getLog(ComitConstant.class);

	/**
	 * Session中当前用户的属性名称
	 */
	public static final String USER_SESSION_ATTRIBUTE = "user";

	/**
	 * 开发模式的jvm参数名称
	 */
	public static final String DEVELOPER_MODE_PARAMETER = "devMode";

	/**
	 * Request中当前执行动作抛出的异常的属性名称
	 */
	public static final String ERROR_EXCEPTION_REQUEST_ATTRIBUTE = "com.comit.common.error.exception";

	/**
	 * Request中执行动作页面URL的属性名称
	 */
	public static final String ERROR_HANDLERURL_REQUEST_ATTRIBUTE = "com.comit.common.error.handlerurl";

	/**
	 * 公共错误处理的配置文件路径
	 */
	public static final String ERROR_PROPERTIES_FILE = "error.properties";

	/**
	 * 公共错误处理的公共错误提示页面的地址配置参数名称，没有配置则默认值为[/error/errorPage.jsp]
	 */
	public static final String ERROR_ERRORPAGE_KEY = "errorPage";

	/**
	 * 公共错误处理的出错提示页面的返回首页的地址配置参数名称，没有配置则默认值为[/index.jsp]
	 */
	public static final String ERROR_INDEXURL_KEY = "indexURL";

	/**
	 * 文件上传下载的配置文件路径
	 */
	public static final String FILEUPLOAD_PROPERTIES_FILE = "fileupload.properties";

	/**
	 * 文件上传路径配置参数名称(多个路径使用英文逗号分割)
	 */
	public static final String FILEUPLOAD_PATH_KEY = "uploadFilePath";

	/**
	 * 文件下载路径配置参数名称(多个路径使用英文逗号分割)
	 */
	public static final String FILEDOWNLOAD_PATH_KEY = "downloadFilePath";

	/**
	 * 获取上传路径数组
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String[] getUploadPath() {
		String uploadFilePath = "";
		try {
			uploadFilePath = PropertiesUtils.getParameter(FILEUPLOAD_PROPERTIES_FILE, FILEUPLOAD_PATH_KEY);
		} catch (Exception e) {
			logger.error("获取上传路径错误：" + e.getMessage(), e);
		}
		return uploadFilePath.split(";");
	}

	/**
	 * 获取下载路径数组
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String[] getDownloadPath() {
		String downloadFilePath = "";
		try {
			downloadFilePath = PropertiesUtils.getParameter(FILEUPLOAD_PROPERTIES_FILE, FILEDOWNLOAD_PATH_KEY);
		} catch (Exception e) {
			logger.error("获取下载路径错误：" + e.getMessage(), e);
		}
		return downloadFilePath.split(";");
	}

	/**
	 * 是否下载路径文件
	 * 
	 * @param file
	 * @return
	 */
	public static boolean isDownloadPath(File file) {
		String tmpdir = System.getProperty("java.io.tmpdir");// 获取临时目录
		File download = new File(tmpdir);
		if (file.getAbsolutePath().startsWith(download.getAbsolutePath())) {// 临时目录允许下载
			return true;
		}
		String[] downloadPaths = getDownloadPath();
		for (String downloadPath : downloadPaths) {
			download = new File(downloadPath);
			if (file.getAbsolutePath().startsWith(download.getAbsolutePath())) {// 下载目录允许下载
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否开发模式，如果设置虚拟机的自定义参数devMode=true，则返回true(使用时配置jvm参数:-DdevMode=true)
	 * 
	 * @return
	 */
	public static boolean isDeveloperMode() {
		return "true".equalsIgnoreCase(System.getProperty(DEVELOPER_MODE_PARAMETER));
	}
	
	/**
	 * 系统默认的管理员主键key,配置在系统配置里
	 */
	public static final String DEFAULT_ADMIN_ID="DEFAULT_ADMIN_ID"; 
}
